package org.xx.armory.commons;

import java.util.concurrent.Callable;

/**
 * {@link Action}和{@link Func}相关的工具类。
 */
public final class Actions {
    private Actions() {
        throw new UnsupportedOperationException();
    }

    public static Callable<Void> callable(Action<?> action) {
        return () -> {
            action.call();
            return null;
        };
    }

    public static <V> Callable<Void> callable(
            Action1<V, ?> action,
            V v
    ) {
        return () -> {
            action.call(v);
            return null;
        };
    }

    public static Runnable runnable(Action<?> action) {
        return () -> {
            try {
                action.call();
            } catch (Exception ex) {
                throw new IllegalStateException(ex);
            }
        };
    }

    public static <V> Runnable runnable(
            Action1<V, ?> action,
            V v
    ) {
        return () -> {
            try {
                action.call(v);
            } catch (Exception ex) {
                throw new IllegalStateException(ex);
            }
        };
    }
}
